/**
 * Candidate number: 35834
 */
package tv.starfu.sequencer.components;

import com.artemis.Component;
// TO-DO
// Implement flyweight pattern from http://java.dzone.com/articles/design-patterns-flyweight

public class TimeVector extends Component implements Comparable {
    private int tick;
    private int beat;
    private int bar;

    public TimeVector() {
        tick = 0;
        beat = 0;
        bar  = 0;
    }

    public TimeVector(int bar, int beat, int tick) {
        this.tick = tick;
        this.beat = beat;
        this.bar = bar;
    }
    
    public void setTime(int bar, int beat, int tick) {
        this.tick = tick;
        this.beat = beat;
        this.bar = bar;
    }
    
    // TO-DO
    /**
     * Converts a TimeVector to milliseconds
     * @param tempo
     * @return 
     */
    public int tvToMs(int tempo) {
        int ms = 0;
        // 
        return ms;
    }
    
    @Override
    public String toString() {
        String print = "[ " + bar + " : " + beat + " : " + tick + " ]";
        return print;
    }
    
    public void tick() {
        
    }

    public int getBar() {
        return bar;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

    public int getBeat() {
        return beat;
    }

    public void setBeat(int beat) {
        this.beat = beat;
    }

    public int getTick() {
        return tick;
    }

    public void setTick(int tick) {
        this.tick = tick;
    }

    public int compareTo(Object t) {
        if(!(t instanceof TimeVector)) {
            throw new ClassCastException("Invalid object.");
        }  

        int bar_1   = bar;
        int beat_1  = beat;
        int tick_1  = tick;

        int bar_2   = ((TimeVector) t).getBar();
        int beat_2  = ((TimeVector) t).getBeat();
        int tick_2  = ((TimeVector) t).getTick();
        
        int result = 0;
        
        // Compare Bars
        if(bar_1 > bar_2) { result =  1; return result; }
            else if(bar_1 < bar_2) { result = -1; }
            else if(bar_1 == bar_2) {
                // Compare Beats
                if(beat_1 > beat_2) { result = 1; return result; }
                else if (beat_1 < beat_2) { result = -1; }
                else if (beat_1 == beat_2) {
                    // Compare Ticks
                    if(tick_1 > tick_2) { result = 1; return result; }
                    else if (tick_1 < tick_2) { result = -1; }
                    else if (tick_1 == tick_2) { result = 0; }
                }
        }

        return result;
        } 
    } 
